package com.lcd.ssm.web.controller;

import com.lcd.ssm.domain.Employee;
import com.lcd.ssm.domain.Menu;
import com.lcd.ssm.page.PageResult;
import com.lcd.ssm.query.EmployeeQueryObject;
import com.lcd.ssm.service.IEmployeeService;
import com.lcd.ssm.service.IMenuService;
import com.lcd.ssm.util.AjaxResult;
import com.lcd.ssm.util.PermissionUtil;
import com.lcd.ssm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IMenuService menuService;

    @RequestMapping("/employee")
    public String index(){
        return "employee";
    }
    @ResponseBody
    @RequestMapping("/employee_list")
    public PageResult queryForPage(EmployeeQueryObject qo){
        return employeeService.queryForPage(qo);
    }

    @ResponseBody
    @RequestMapping("/login")
    public AjaxResult login(String username, String password, HttpServletRequest request){
        UserContext.setRequest(request);
        AjaxResult ajaxResult=null;
        Employee employee=employeeService.getEmployeeForLogin(username,password);
        Map<String, Object> map = new HashMap<>();
        if (employee == null) {
            ajaxResult = new AjaxResult("帐号密码不正确");
        }else {
            ajaxResult = new AjaxResult(true,"登陆成功");
            //把当前用户所对应的permission放到session里面去
            List<String> permissionList=employeeService.findPermissionStrByEmployeeId(employee.getId());
            request.getSession().setAttribute(UserContext.PERMISSIONS_SESSION,permissionList);
            request.getSession().setAttribute(UserContext.USER_SESSION,employee);
            //判断该用户的菜单的权限，然后放到session里面
            List<Menu> menus = menuService.queryForRoot();
            PermissionUtil.checkCurrentUserMenu(menus);
            request.getSession().setAttribute(UserContext.MENU_SESSION,menus);

        }
        return ajaxResult;
    }

    @RequestMapping("/employee_save")
    @ResponseBody
    public AjaxResult save(Employee employee) {
        AjaxResult ajaxResult=null;
        try{
            employee.setAdmin(false);
            employee.setState(true);
            employee.setPassword("123");
            employeeService.insert(employee);
            ajaxResult = new AjaxResult(true,"保存成功");
        }catch (Exception e){
            ajaxResult = new AjaxResult("保存失败");
        }
        return ajaxResult;
    }

    @RequestMapping("/employee_update")
    @ResponseBody
    public AjaxResult update(Employee employee) {
        AjaxResult ajaxResult=null;
        try{
            employeeService.updateByPrimaryKey(employee);
            ajaxResult = new AjaxResult(true,"修改成功");
        }catch (Exception e){
            ajaxResult = new AjaxResult("修改失败");
        }
        return ajaxResult;
    }

    @RequestMapping("/employee_delete")
    @ResponseBody
    public AjaxResult delete(Long id) {
        AjaxResult ajaxResult=null;
        try{
            employeeService.updateStateByPrimaryKey(id);
            ajaxResult = new AjaxResult(true,"删除成功");
        }catch (Exception e){
            ajaxResult = new AjaxResult("删除失败");
        }
        return ajaxResult;
    }

    @RequestMapping("/queryRoleIdByEmployeeId")
    @ResponseBody
    public List<Long> queryRoleIdByEmployeeId(Long eid) {
        return employeeService.queryRoleIdByEmployeeId(eid);
    }

}
